package jian.hamster.game
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import jian.hamster.controller.GameController;
	import jian.hamster.controller.TimerController;
	import jian.hamster.event.GameEvent;
	import jian.hamster.ui.CountDownTxt;
	import jian.hamster.ui.JProgressBar;
	
	/**
	 * 
	 * @author Jian
	 * 
	 */	
	public class HamsterGame extends Sprite
	{
		private static const HOLES:Array = new Array;
		private static const ROW:int = 4;
		private static const COL:int = 6;
		private var _hamster:Hamster;
		private var _scorePanel:TextField;
		public function HamsterGame()
		{
			super();
			var cdTxt:CountDownTxt = new CountDownTxt(1 * 60);
			layoutObj(cdTxt,420);
			
			var progressBar:JProgressBar = new JProgressBar(0x0000FF,500,30,60,30,10);
			progressBar.x=(640-progressBar.width) >> 1;
			layoutObj(progressBar,440);
			
			var map:GameMap = new GameMap();
			layoutObj(map,10);
			
			_scorePanel = new TextField;
			_scorePanel.text = "SCORE: ";
			layoutObj(_scorePanel,420, 50);
			
			GameController.instance.addEventListener(GameEvent.SCORE, onScoreUpdate);
			
			
			for (var i:int = 0; i<COL;i++)
			{
				HOLES[i] = new Array;
				for (var j:int=0; j<ROW;j++)
				{
					var hole:Hole = new Hole(map.x + i*100 +50, map.y+j*100 +50);
					addChild(hole);
					HOLES[i][j] = hole;
					hole.addEventListener(MouseEvent.CLICK, onHoleClick);
					hole.move();
				}
			}
			
			TimerController.instance.addTickerCallBack(40,gameEnterFrame);
		}
		
		protected function onScoreUpdate(event:GameEvent):void
		{
			_scorePanel.text = "SCORE: " + event.data; 			
		}
		
		/**
		 * 点击洞
		 * @param event
		 * 
		 */		
		protected function onHoleClick(event:MouseEvent):void
		{
			if(event.currentTarget is Hole)
			{
				var clickHole:Hole = event.currentTarget as Hole;
				if(clickHole.contains(_hamster))
				{
					_hamster.state = LivingState.HURT;
				}
					
			}
		}
		
		private function gameEnterFrame():void
		{
			GameController.instance.dispatchEvent(new GameEvent(GameEvent.ENTER_FRAME));
			if(!_hamster)
			{
				_hamster = new Hamster;
//				_hamster.addEventListener(MouseEvent.CLICK, __onClickHamster);
			}
			
			if(contains(_hamster))
			{
				_hamster.doState();
			}
			if(_hamster.state == LivingState.DIE)
			{
				_hamster.state = LivingState.STAY;
				var hole:Hole = HOLES[int(Math.random()*COL)][int(Math.random()*ROW)];
				_hamster.x = 2.5;			
				_hamster.y = 2.5;
				hole.addChild(_hamster);
			}
		}
		
		protected function __onClickHamster(event:MouseEvent):void
		{
			if(_hamster)
			{
				_hamster.state = LivingState.HURT;
			}
		}
		
		private function layoutObj($obj:DisplayObject, $y:int=0, $x=-1):void
		{
			addChild($obj);
			$obj.x = $x == -1 ? (640-$obj.width) >> 1 : $x;
			$obj.y= $y;
		}
		
	}
}